package cz.zcu.fav.rat.rest.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.beans.Reservation;
import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.dataaccess.service.ReservationService;
import cz.zcu.fav.rat.rest.errorhandling.UnknownResourceException;
import cz.zcu.fav.rat.rest.factory.ReservationsMappingFactory;
import cz.zcu.fav.rat.rest.utils.RestUtils;

@Controller
@RequestMapping("/rest/reservations")
public class ReservationRestController
{

	@Autowired
	ReservationService reservationService;

	@Autowired
	ReservationsMappingFactory reservationsMappingFactory;

	String XML_VIEW_NAME = "reservationRestView";

	@RequestMapping(method = RequestMethod.GET, value = "/reservation")
	public ModelAndView getUserReservations(
			@RequestParam(value = "id", required = true) Integer id)
			throws UnknownResourceException
	{
		// List<Period> periods =
		// periodService.getAllPeriodsByUserIdInDateRange(
		// user.getId(), fromDate, toDate);
		Reservation reservation = reservationService.get(id);

		if (reservation == null)
		{
			throw new UnknownResourceException("Reservation cannot be found");
		}

		Map<Reservation, Collection<Period>> reservationMap = new HashMap<Reservation, Collection<Period>>();
		reservationMap.put(reservation, reservation.getPeriods());
		return new ModelAndView(XML_VIEW_NAME, "reservation",
				reservationsMappingFactory.getMappedObject(reservationMap));
	}
}
